﻿using System;
using System.Collections.Generic;
using AMM.Business.Store;
using AMM.Model;
using AMM.Model.Finance;
using AMM.Model.Store;

namespace AMM.UserControls {
    public partial class StatementSearchControl : BaseSearchControl {

        public override BaseSearchModel GetSearchModel() {
            int vendorValue = Convert.ToInt32(this.ddlVendor.SelectedValueEx);
            int statusValue = Convert.ToInt32(this.cmbStatus.SelectedValue);
            DateTime? dt = this.GetDateTimePickerValue(this.dtpDatetimeSpan);

            StatementSearchModel searchResult = new StatementSearchModel();
            searchResult.StatementCode = this.tbCode.Text;
            searchResult.VendorId = vendorValue;
            searchResult.Status = statusValue;

            if (dt.HasValue) {
                searchResult.Year = dt.Value.Year;
                searchResult.Month = dt.Value.Month;
            }
            if (vendorValue == 0) { searchResult.VendorId = null; }
            if (statusValue == -1) { searchResult.Status = null; }

            return searchResult;
        }

        public override void ResetSearchModel() {
            this.ddlVendor.SelectedValueEx = null;
            this.tbCode.Clear();
            this.cmbStatus.SelectedIndex = 0;
            this.dtpDatetimeSpan.Checked = false;
        }

        private void BindVendor() {
            VendorSearchModel searchModel = new VendorSearchModel() { IsEnabled = true };
            VendorProcessor vendorProcessor = new VendorProcessor(int.MaxValue);
            List<VendorViewModel> vendorList = vendorProcessor.FetchVendorViewModelList(searchModel);

            this.ddlVendor.DisplayMemberEx = "Code";
            this.ddlVendor.ValueMemberEx = "Key";
            this.ddlVendor.HeightEx = 300;
            this.ddlVendor.WidthEx = 400;
            this.ddlVendor.PageSizeEx = 20;
            this.ddlVendor.DataBind(new VendorViewModel(), x => new { x.Code, x.Name, x.Address }, vendorList);
        }       

        public StatementSearchControl() {
            this.InitializeComponent();
            this.BindAuditStatus(this.cmbStatus);
        }

        private void StatementSearchControl_Load(object sender, EventArgs e) {
            this.BindVendor();
        }
    }
}
